package cn.wh.user.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.stereotype.Repository;

import cn.wh.common.Util.ConstantUtil;
import cn.wh.common.Util.PageInfoUtil;
import cn.wh.common.dao.BaseDaoImpl;
import cn.wh.user.pojo.ARole;

/**
 * 角色的dao实现类
 */
@Repository("roleDao")
public class ARoleDaoImpl extends BaseDaoImpl<ARole> implements IARoleDao
{

	// 查询一条角色
	// condMap 从页面上传递过来的查询条件
	@Override
	public ARole getOne(Map<String, Object> condMap)
	{
		// hql语句
		StringBuffer hql = new StringBuffer();
		hql.append("from ARole ad where 1 = 1 ");     // 这里要写类的名称
		// hibernate要求的查询条件
		Map<String, Object> paramMap = new HashMap<String, Object>();
		// 按照id查询
		if (condMap.get("id") != null && !"".equals(condMap.get("id"))) {
			hql.append(" and ad.id = :id ");
			paramMap.put("id", Integer.valueOf(condMap.get("id") + ""));
		}
		// 按照email查询
		if (condMap.get("email") != null && !"".equals(condMap.get("email"))) {
			hql.append(" and ad.email = :email ");
			paramMap.put("email", condMap.get("email"));
		}
		
		// 排序
		if("createASC".equals(condMap.get("orderby") + ""))  {
			hql.append(" order by createDate asc ");
		} else {
			hql.append(" order by updateDate asc ");
		}
		
		return this.getHibernateTemplate().execute(new HibernateCallback<ARole>()
		{
			@Override
			public ARole doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery(hql.toString());
				// 设置查询有条件
				query.setProperties(paramMap);
				return (ARole) query.uniqueResult();
			}
		});
	}

	// 查询多条角色
	@Override
	public List<ARole> getList(Map<String, Object> condMap,PageInfoUtil pageInfoUtil)
	{
		// hql语句
		StringBuffer hql = new StringBuffer();
		hql.append("from ARole ad where 1 = 1 ");     // 这里要写类的名称
		// hibernate要求的查询条件
		Map<String, Object> paramMap = new HashMap<String, Object>();
		// 按照id查询
		/*if (condMap.get("id") != null && !"".equals(condMap.get("id"))) {
			hql.append(" and ad.id = :id ");
			paramMap.put("id", Integer.valueOf(condMap.get("id") + ""));
		}*/
		// 按照关键字搜索
		if (condMap.get("keyword") != null && !"".equals(condMap.get("keyword"))) {
			hql.append(" and (ad.email like :keyword or ad.trueName like :keyword or ad.phone like :keyword)");
			paramMap.put("keyword", "%" + condMap.get("keyword") + "%");
		}
		// 按照状态搜索
		if (condMap.get("status") != null && !"".equals(condMap.get("status"))) {
			hql.append(" and ad.status = :status ");
			paramMap.put("status", Byte.valueOf(condMap.get("status") + ""));
		}
		// 按照更新时间搜索
		if (condMap.get("startDate") instanceof Date &&condMap.get("endDate") instanceof Date ) {
			hql.append(" and ad.updateDate  >= :startDate and ad.updateDate < :endDate");
			paramMap.put("startDate", condMap.get("startDate") );
			paramMap.put("endDate", condMap.get("endDate") );
		}
		// 排序
		if("createASC".equals(condMap.get("orderby") + ""))  {
			hql.append(" order by createDate asc ");
		} else {
			hql.append(" order by updateDate asc ");
		}
		// 是否分页
		if (pageInfoUtil != null) {
			// 分页
			return this.returnGetListByPage(hql.toString(), paramMap, pageInfoUtil);
			/*return this.getHibernateTemplate().execute(new HibernateCallback<List<ARole>>()
			{
				@Override
				public List<ARole> doInHibernate(Session session) throws HibernateException
				{
					// 先查询总记录数select count(*) from ARole where 1 = 1 and id = 1
					String countHql = "select count(*) " + hql.substring(hql.indexOf("from"),hql.indexOf("order by "));
					// 查询总记录数
					Query query = session.createQuery(countHql);
					// 设置查询有条件
					query.setProperties(paramMap);
					// 总记录数返回结果
					Long totalRecord = (Long) query.uniqueResult();
					// 设置总记录数
					pageInfoUtil.setTotalRecord(totalRecord.intValue());
					
					// 查询分页数据from ARole where 1 = 1 and id = 1 order by createDate desc limit ?,?
					Query query2 = session.createQuery(hql.toString());
					// 设置查询有条件
					query2.setProperties(paramMap);
					// 设置分页条件
					// limit第一个问号：当前页条数
					query2.setFirstResult(pageInfoUtil.getCurrentRecord());
					// limit第二个问号：每页多少条
					query2.setMaxResults(pageInfoUtil.getPageSize());
					
					return query2.list();
				}
			});*/
		} else {
			// 不分页
			return returnGetListNoPage(hql.toString(), paramMap);
			/*return this.getHibernateTemplate().execute(new HibernateCallback<List<ARole>>()
			{
				@Override
				public List<ARole> doInHibernate(Session session) throws HibernateException
				{
					Query query = session.createQuery(hql.toString());
					// 设置查询有条件
					query.setProperties(paramMap);
					return query.list();
				}
			});*/
		}
		
		// 返回一个空集合
		//return Collections.emptyList();
	}
}
